<template>
  <div class="chat-header">
    <div class="chat-info">
      <img :src="currentChat.avatar" class="avatar" />
      <div class="chat-details">
        <div class="chat-title">{{ currentChat.name }}</div>
        <div v-if="currentChat.type === 'private'" class="chat-status">
          <span :class="['status-dot', friendStatus]"></span>
          {{ statusText }}
        </div>
        <div v-else class="chat-members">
          {{ groupMembers.length }} 成员
        </div>
      </div>
    </div>
    <div class="chat-actions">
      <button class="btn-icon">
        <Icon icon="mdi:call" />
      </button>
      <button class="btn-icon">
        <Icon icon="mdi:video" />
      </button>
      <button class="btn-icon">
        <Icon icon="mdi:more" />
      </button>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { Icon } from '@iconify/vue';
import { useChatStore } from '@/stores/chat'

const chatStore = useChatStore()
const currentChat = computed(() => chatStore.currentChat)

const friendStatus = computed(() => {
  if (currentChat.value.type !== 'private') return ''
  const friend = chatStore.friends.find(f => f.id === currentChat.value.id)
  return friend ? friend.status : 'offline'
})

const statusText = computed(() => {
  if (currentChat.value.type !== 'private') return ''
  const friend = chatStore.friends.find(f => f.id === currentChat.value.id)
  if (!friend) return '离线'
  
  if (friend.status === 'online') {
    return '在线'
  } else {
    return `最后在线: ${formatTime(friend.lastSeen)}`
  }
})

const groupMembers = computed(() => {
  if (currentChat.value.type !== 'group') return []
  const group = chatStore.groups.find(g => g.id === currentChat.value.id)
  return group ? group.members : []
})

const formatTime = (time) => {
  const date = new Date(time)
  return date.toLocaleTimeString()
}
</script>

<style scoped>
.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #e0e0e0;
  background-color: white;
}

.chat-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  margin-right: 0.75rem;
}

.chat-details {
  display: flex;
  flex-direction: column;
}

.chat-title {
  font-weight: 500;
}

.chat-status,
.chat-members {
  font-size: 0.75rem;
  color: #999;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 0.25rem;
}

.status-dot.online {
  background-color: #07c160;
}

.status-dot.offline {
  background-color: #999;
}

.chat-actions {
  display: flex;
  gap: 0.5rem;
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 4px;
}

.btn-icon:hover {
  background-color: #f0f0f0;
}
</style>